package com.quan.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quan.commons.core.biz.support.MyBaseServiceImpl;
import com.quan.commons.core.utils.PageUtils;
import com.quan.mall.commons.vo.PmsBrandVo;
import com.quan.mall.entity.PmsBrand;
import com.quan.mall.entity.PmsCategory;
import com.quan.mall.service.PmsBrandService;
import com.quan.mall.service.PmsCategoryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.quan.mall.commons.vo.PmsCategoryBrandRelationVo;
import com.quan.mall.entity.PmsCategoryBrandRelation;
import com.quan.mall.mapper.PmsCategoryBrandRelationMapper;
import com.quan.mall.service.PmsCategoryBrandRelationService;


@Slf4j
@Service
public class PmsCategoryBrandRelationServiceImpl extends MyBaseServiceImpl<PmsCategoryBrandRelationMapper, PmsCategoryBrandRelation> implements PmsCategoryBrandRelationService {

    @Autowired
	private PmsCategoryBrandRelationMapper mapper;

    @Autowired
    private PmsBrandService pmsBrandService;

    @Autowired
    private PmsCategoryService pmsCategoryService;

	@Override
    public List<PmsCategoryBrandRelationVo> queryList(PageUtils pageUtils, PmsCategoryBrandRelationVo vo) {
        QueryWrapper<PmsCategoryBrandRelation> queryWrapper = new QueryWrapper<PmsCategoryBrandRelation>(vo);

        List<PmsCategoryBrandRelation> list = super.list(pageUtils, queryWrapper);
        List<PmsCategoryBrandRelationVo> collect = list.stream().map(item -> {
            PmsCategoryBrandRelationVo bean = new PmsCategoryBrandRelationVo();
            BeanUtils.copyProperties(item, bean);
            return bean;
        }).collect(Collectors.toList());

        return collect;
    }

    @Override
    public PageUtils queryPage(PageUtils pageUtils, PmsCategoryBrandRelationVo vo) {
        QueryWrapper<PmsCategoryBrandRelation> queryWrapper = new QueryWrapper<PmsCategoryBrandRelation>(vo);

        if(StringUtils.isNotBlank(vo.getKeyword())) {
            queryWrapper.and(w -> {
                w.like("username", vo.getKeyword());
            });
        }

        final IPage<PmsCategoryBrandRelation> page = super.selectPage(pageUtils, queryWrapper);

        PageUtils ps = new PageUtils(page);

        final List<PmsCategoryBrandRelation> records = page.getRecords();

        final List<PmsCategoryBrandRelation> collect = records.stream().map((item) -> {
            PmsCategoryBrandRelationVo bean = new PmsCategoryBrandRelationVo();
            BeanUtils.copyProperties(item, bean);
            return bean;
        }).collect(Collectors.toList());

        ps.setRecords(collect);
        return ps;
    }

    @Override
    public void updateBrand(Long brandId, String name) {
        PmsCategoryBrandRelation brandRelation = new PmsCategoryBrandRelation();
        brandRelation.setBrandId(brandId);
        brandRelation.setBrandName(name);
        this.update(brandRelation,new UpdateWrapper<PmsCategoryBrandRelation>().eq("brand_id",brandId));
    }

    @Override
    public void updateCategory(Long cateId, String name) {
        UpdateWrapper<PmsCategoryBrandRelation> updateWrapper = new UpdateWrapper<PmsCategoryBrandRelation>();
        updateWrapper.set("cate_name", name).eq("cate_id", cateId);
        super.update(updateWrapper);
    }

    @Override
    public void saveDetail(PmsCategoryBrandRelation entity) {
        Long brandId = entity.getBrandId();
        Long catelogId = entity.getCateId();

        //1、查询品牌详细信息
        PmsBrand brandEntity = pmsBrandService.getById(brandId);
        //2、查询分类详细信息
        PmsCategory category = pmsCategoryService.getById(catelogId);

        //将信息保存到categoryBrandRelation中
        entity.setBrandName(brandEntity.getName());
        entity.setCateName(category.getName());

        // 保存到数据库中
        super.save(entity);
    }

    @Override
    public List<PmsBrandVo> getBrandsByCatId(Long cateId) {
        List<PmsCategoryBrandRelation> categoryBrandRelations = super.list(new QueryWrapper<PmsCategoryBrandRelation>().eq("cate_id", cateId));

        List<PmsBrandVo> collect = categoryBrandRelations.stream().map(item -> {
            Long brandId = item.getBrandId();
            //查询品牌的详情
            PmsBrand byId = this.pmsBrandService.getById(brandId);

            PmsBrandVo vo =  new PmsBrandVo();
            BeanUtils.copyProperties(byId, vo);
            return vo;
        }).collect(Collectors.toList());

        return collect;
    }
}